package mwa.cs.admin.ctrl;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import mwa.cs.dao.AddressDAO;
import mwa.cs.dao.ArDAO;
import mwa.cs.dao.CustHHDAO;
import mwa.cs.dao.CustMonthlyDAO;
import mwa.cs.dao.GenDocDAO;
import mwa.cs.dao.SMErrorLogDAO;
import mwa.cs.dao.SMRoleDAO;
import mwa.cs.dao.SMUserRoleDAO;
import mwa.cs.dao.SitesDAO;
import mwa.cs.dao.UserLoginDAO;
import mwa.cs.database.Database;
import mwa.cs.hh.biz.MeterFunction;
import mwa.cs.model.Ar;
import mwa.cs.model.CustHH;
import mwa.cs.model.CustMonthly;
import mwa.cs.model.SMUserRole;
import mwa.cs.model.UserLogin;
import mwa.cs.util.Encryption;
import mwa.cs.util.JDate;

/**
 * Servlet implementation class CS83001Srvl
 */
public class CS83001Srvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Database db ;
	private String vaAgentBrowser;
	private String vaUrlAddress;
	private int vnUserId;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CS83001Srvl() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	String vaProcessType = request.getParameter("process_type");
    	// get site_id
    	vnUserId = Integer.parseInt(request.getSession().getAttribute("user_id").toString());
    	vaUrlAddress = request.getParameter("url_address");
    	vaAgentBrowser = request.getParameter("agent_browser");
    	if(vaProcessType.equals("checkUserDuplicate")){
    		checkUserDuplicate(request,response);
    	}else if(vaProcessType.equals("getProvince")){
    		getProvince(request,response);
    	}else if (vaProcessType.equals("getDistrict")){
    		getDistrict(request,response);
    	}else if(vaProcessType.equals("getCityCode")){
    		getCityCode(request,response);
    	}else if(vaProcessType.equals("getSite")){
    		getSite(request,response);
    	}else if(vaProcessType.equals("getRole")){
    		getRole(request,response);
    	}else if (vaProcessType.equals("insert")){
    		insertData(request,response);
    	} else if (vaProcessType.equals("showDataEdit")){
    		showEditData(request,response);
    	} else if (vaProcessType.equals("edit")) {
    		editData(request,response);
    	}
    }

    @SuppressWarnings("unchecked")
    public void getCityCode(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		String vaProvince = request.getParameter("proVince");
		String vaDistrict = request.getParameter("disTrict");
		String[] vaMessage = null, vaValue = null;
    	this.db = new Database();
    	
    	AddressDAO voAddress = new AddressDAO(db);
		List<Map<String, Object>> voResult = voAddress.findCity(vaProvince, vaDistrict);
		vaValue = new String[voResult.size()];
		vaMessage = new String[voResult.size()];
		for (int i = 0; i < voResult.size(); i++) {
			Map<String, Object> voObject = voResult.get(i);	
			vaValue[i] = (String) voObject.get("CITY"); 
			vaMessage[i] = (String) voObject.get("NAME");
		}	
		JSONObject voObject = new JSONObject();
		JSONArray voList = new JSONArray();
		PrintWriter voOut = response.getWriter();
		if (vaMessage != null) {
			for (int i = 0; i < vaMessage.length; i ++) {
				voList.add(vaMessage[i]);
			}
		}
		voObject.put("messages", voList);
		JSONArray voList2 = new JSONArray();
		if (vaValue != null) {
			for (int i = 0; i < vaValue.length; i ++) {
				voList2.add(vaValue[i]);
			}
		}
		voObject.put("values", voList2);
		db.close();
		voOut.print(voObject.toJSONString());
    }
    
    @SuppressWarnings("unchecked")
    public void getDistrict(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		String vaProvince = request.getParameter("proVince");
		
		String[] vaMessage = null, vaValue = null;
    	this.db = new Database();
    	
    	AddressDAO voAddress = new AddressDAO(db);
		List<Map<String, Object>> voResult = voAddress.findDistrict(vaProvince);
		vaValue = new String[voResult.size()];
		vaMessage = new String[voResult.size()];
		for (int i = 0; i < voResult.size(); i++) {
			Map<String, Object> voObject = voResult.get(i);	
			vaValue[i] = (String) voObject.get("DISTRICT"); 
			vaMessage[i] = (String) voObject.get("NAME");
			//System.voOut.println(vaValue[i]+"  "+vaMessage[i]);
		}
		JSONObject voObject = new JSONObject();
		JSONArray voList = new JSONArray();
		PrintWriter voOut = response.getWriter();
		if (vaMessage != null) {
			for (int i = 0; i < vaMessage.length; i ++) {
				voList.add(vaMessage[i]);
			}
		}
		voObject.put("messages", voList);
		JSONArray voList2 = new JSONArray();
		if (vaValue != null) {
			for (int i = 0; i < vaValue.length; i ++) {
				voList2.add(vaValue[i]);
			}
		}
		voObject.put("values", voList2);
		db.close();
		voOut.print(voObject.toJSONString());
    }
    
    @SuppressWarnings("unchecked")
    public void getProvince(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		
    	String vaProvince = request.getParameter("province");
    	String[] vaMessage = null, vaValue = null;
    	this.db = new Database();
    	if (vaProvince != null && vaProvince.equals("yes")) {
			AddressDAO voAddress = new AddressDAO(db);
			List<Map<String, Object>> voResult = voAddress.findProvince();
			vaValue = new String[voResult.size()];
			vaMessage = new String[voResult.size()];
			for (int i = 0; i < voResult.size(); i++) {
				Map<String, Object> voObject = voResult.get(i);	
				vaValue[i] = (String) voObject.get("PROVINCE"); 
				vaMessage[i] = (String) voObject.get("NAME");	
				//System.voOut.println(vaValue[i]+"  "+vaMessage[i]);
			}
    	}
    	JSONObject voObject = new JSONObject();
		JSONArray voList = new JSONArray();
		PrintWriter voOut = response.getWriter();
		if (vaMessage != null) {
			for (int i = 0; i < vaMessage.length; i ++) {
				voList.add(vaMessage[i]);
			}
		}
		voObject.put("messages", voList);
		JSONArray voList2 = new JSONArray();
		if (vaValue != null) {
			for (int i = 0; i < vaValue.length; i ++) {
				voList2.add(vaValue[i]);
			}
		}
		voObject.put("values", voList2);
		db.close();
		voOut.print(voObject.toJSONString());
    }
    
    @SuppressWarnings("unchecked")
    public void getSite(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		
    	String vaSite = request.getParameter("site");
    	String[] vaMessage = null, vaValue = null;
    	this.db = new Database();
    	if (vaSite != null && vaSite.equals("yes")) {
			SitesDAO voSite = new SitesDAO(db);
			List<Map<String, Object>> voResult = voSite.findSite();
			vaValue = new String[voResult.size()];
			vaMessage = new String[voResult.size()];
			for (int i = 0; i < voResult.size(); i++) {
				Map<String, Object> voObject = voResult.get(i);	
				vaValue[i] = ((BigDecimal) voObject.get("SITE_ID")).toString();
				vaMessage[i] = (String) voObject.get("SITE_NAME");	
				//System.voOut.println(vaValue[i]+"  "+vaMessage[i]);
			}
    	}
    	JSONObject voObject = new JSONObject();
		JSONArray voList = new JSONArray();
		PrintWriter voOut = response.getWriter();
		if (vaMessage != null) {
			for (int i = 0; i < vaMessage.length; i ++) {
				voList.add(vaMessage[i]);
			}
		}
		voObject.put("messages", voList);
		JSONArray voList2 = new JSONArray();
		if (vaValue != null) {
			for (int i = 0; i < vaValue.length; i ++) {
				voList2.add(vaValue[i]);
			}
		}
		voObject.put("values", voList2);
		db.close();
		voOut.print(voObject.toJSONString());
    }
    
    @SuppressWarnings("unchecked")
    public void getRole(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		String vaSite = request.getParameter("vaSite");
		String[] vaMessage = null, vaValue = null;
    	this.db = new Database();
    	
    	SMRoleDAO voSMRoleDAO = new SMRoleDAO(db);
		List<Map<String, Object>> voResult = voSMRoleDAO.findRole(vaSite);
		vaValue = new String[voResult.size()];
		vaMessage = new String[voResult.size()];
		for (int i = 0; i < voResult.size(); i++) {
			Map<String, Object> voObject = voResult.get(i);	
			vaValue[i] = voObject.get("ROLE_ID").toString();
			vaMessage[i] = (String) voObject.get("ROLE_NAME");
			//System.voOut.println(vaValue[i]+"  "+vaMessage[i]);
		}
		JSONObject voObject = new JSONObject();
		JSONArray voList = new JSONArray();
		PrintWriter voOut = response.getWriter();
		if (vaMessage != null) {
			for (int i = 0; i < vaMessage.length; i ++) {
				voList.add(vaMessage[i]);
			}
		}
		voObject.put("messages", voList);
		JSONArray voList2 = new JSONArray();
		if (vaValue != null) {
			for (int i = 0; i < vaValue.length; i ++) {
				voList2.add(vaValue[i]);
			}
		}
		voObject.put("values", voList2);
		db.close();
		voOut.print(voObject.toJSONString());
    }
    
    @SuppressWarnings("unchecked")
    public void checkUserDuplicate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		String userName = request.getParameter("vaUsername");
    	this.db = new Database();
    	JSONObject voObject = new JSONObject();
		PrintWriter voOut = response.getWriter();
    	UserLoginDAO voUserLoginDAO = new UserLoginDAO(db);
		Map<String, Object> voResult = voUserLoginDAO.findUserByUserName(userName);
		if (voResult != null) {
			voObject.put("resultUser", "duplicateUser");
		} else {
			voObject.put("resultUser", "noDuplicateUser");
		}
		db.close();
		voOut.print(voObject.toJSONString());
    }
    
    @SuppressWarnings("unchecked")
	public void insertData(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		
		PrintWriter voOut = response.getWriter();
		JSONObject voObject = new JSONObject();
		this.db = new Database();
		UserLoginDAO voUserLoginDAO = new UserLoginDAO(db);
		SMUserRoleDAO voSmUserRoleDAO = new SMUserRoleDAO(db);
		GenDocDAO voGenDocDAO = new GenDocDAO(db);
		Encryption voEncryption = new Encryption();
		UserLogin voUserLogin = new UserLogin();
		SMUserRole voSMUserRole = new SMUserRole();
		String vaUserId = voGenDocDAO.generatingNextUIDoc();
		String vaUserPName = request.getParameter("vaUserPName");
		String vaUserFName = request.getParameter("vaUserFName");
		String vaUserLName = request.getParameter("vaUserLName");
		String vaBirthDay = request.getParameter("vaBirthDay");
		String vaHomeNumber = request.getParameter("vaHomeNumber");
		String vaAddress = request.getParameter("vaAddress");
		String vaCity = request.getParameter("vaCity");
		String vaDistrict = request.getParameter("vaDistrict");
		String vaProvince = request.getParameter("vaProvince");
		String vaPostCode = request.getParameter("vaPostCode");
		String vaTelephone = request.getParameter("vaTelephone");
		String vaEmailAddress = request.getParameter("vaEmailAddress");
		String vaStartWork = request.getParameter("vaStartWork");
		String vaEndWork = request.getParameter("vaEndWork");
		String vaPosition = request.getParameter("vaPosition");
		String vaChiefPosition = request.getParameter("vaChiefPosition");
		String vaStatus = request.getParameter("vaStatus");
		String vaSite = request.getParameter("vaSite");
		String vaRole = request.getParameter("vaRole");
		String vaUsername = request.getParameter("vaUsername");
		String vaPassword = voEncryption.md5Encrypt(voEncryption.dataEncrypt(request.getParameter("vaPassword")));
		String vaPasswordKey = "";
		String vaLoginKey = "";
		
		// set ค่า LoginUser
		voUserLogin.setVnSiteId(Integer.parseInt(vaSite));
		voUserLogin.setVaUserId(Integer.parseInt(vaUserId));
		voUserLogin.setVaUserPName(vaUserPName.equals("") ? "" : vaUserPName);
		voUserLogin.setVaUserFName(vaUserFName.equals("") ? "" : vaUserFName);
		voUserLogin.setVaUserLName(vaUserLName.equals("") ? "" : vaUserLName);
		voUserLogin.setVaBirthDay(vaBirthDay.equals("") ? "" : JDate.saveDate(vaBirthDay));
		voUserLogin.setVaHomeNumber(vaHomeNumber.equals("") ? "" : vaHomeNumber);
		voUserLogin.setVaAddress(vaAddress.equals("") ? "" : vaAddress);
		voUserLogin.setVaCityCode(vaCity.equals("") ? "" : vaCity);
		voUserLogin.setVaDistrictCode(vaDistrict.equals("") ? "" : vaDistrict);
		voUserLogin.setVaProvinceCode(vaProvince.equals("") ? "" : vaProvince);
		voUserLogin.setVaPostCode(vaPostCode.equals("") ? "" : vaPostCode);
		voUserLogin.setVaTelephone(vaTelephone.equals("") ? "" : vaTelephone);
		voUserLogin.setVaEmailAddress(vaEmailAddress.equals("") ? "" : vaEmailAddress);
		voUserLogin.setVaStartWorking(vaStartWork.equals("") ? "" : JDate.saveDate(vaStartWork));
		voUserLogin.setVaEndWorking(vaEndWork.equals("") ? "" : JDate.saveDate(vaEndWork));
		voUserLogin.setVaUserPosition(vaPosition.equals("") ? "" : vaPosition);
		voUserLogin.setVaHeadPosition(vaChiefPosition.equals("") ? "" : vaChiefPosition);
		voUserLogin.setVaUserStatus(vaStatus.equals("") ? "" : vaStatus);
		voUserLogin.setVaUsername(vaUsername.equals("") ? "" : vaUsername);
		voUserLogin.setVaUserPassword(vaPassword.equals("") ? "" : vaPassword);
		voUserLogin.setVaPasswordKey(vaPasswordKey);
		voUserLogin.setVaLoginKey(vaLoginKey);
		voUserLogin.setVnInsertUserId(vnUserId);
		voUserLogin.setVaInsertDate(JDate.getDate());
		voUserLogin.setVaInsertTime(JDate.getTime());
		voUserLogin.setVnUpdateUserId(0);

		// set ค่า SMUserRole
		voSMUserRole.setVnUserId(Integer.parseInt(vaUserId));
		voSMUserRole.setVaRoleId(vaRole);
		voSMUserRole.setVnInsertUserId(vnUserId);
		voSMUserRole.setVaInsertDate(JDate.getDate());
		voSMUserRole.setVaInsertTime(JDate.getTime());
		voSMUserRole.setVnUpdateUserId(0);
		
		try{
			db.beginTransaction();
			boolean result = false;
			result = voUserLoginDAO.insertUserLogin(voUserLogin);
			if (result) {
				result = voSmUserRoleDAO.insertUserRole(voSMUserRole);
				if (result) {
					voObject.put("resultData", "success");
					db.commit();
				} else {
					voObject.put("resultData", "fail");
				}
			}
			db.commit();
		} catch (Exception e) {
			SMErrorLogDAO.getInstance().addLog(0, "CS83001", JDate.getDate(), JDate.getTime(),vnUserId, "CS83001Srvl", e.getMessage(),vaUrlAddress, vaAgentBrowser);
			db.rollback();
			voObject.put("resultData", "fail");
		}
		db.close();
		voOut.print(voObject.toJSONString());
	}
    
    @SuppressWarnings("unchecked")
    public void showEditData(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/JSON");
		
		this.db = new Database();
		PrintWriter voOut = response.getWriter();
		JSONObject voObject = new JSONObject();
		String userIdEdit = request.getParameter("userIdEdit");
		String vaPName = "";
		String vaFName = "";
		String vaLName = "";
		String vaBirthday = "";
		String vaHomeNumber = "";
		String vaAddress = "";
		String vaCity = "";
		String vaDistrict = "";
		String vaProvince = "";
		String vaPostCode = "";
		String vaTelephone = "";
		String vaEmailAddress = "";
		String vaStartWorking = "";
		String vaEndWorking = "";
		String vaUserPosition = "";
		String vaHeadPosition = "";
		String vaUserStatus = "";
		String vaSite = "";
		String vaRole = "";
		String vaUserName = "";
		UserLoginDAO voUserLoginDAO = new UserLoginDAO(db);
		Map<String, Object> voResult = voUserLoginDAO.getUserLoginAllForEdit(userIdEdit);
		SMUserRoleDAO voSMUserRoleDAO = new SMUserRoleDAO(db);
		Map<String, Object> voResultUserRole = voSMUserRoleDAO.getUserRoleAllForEdit(userIdEdit);
		
		vaPName = "" + voResult.get("USER_PNAME");
		vaFName = "" + voResult.get("USER_FNAME");
		vaLName = "" + voResult.get("USER_LNAME");
		vaBirthday = "" + voResult.get("BIRTHDAY");
		vaHomeNumber = "" + voResult.get("HOME_NUMBER");
		vaAddress = "" + voResult.get("ADDRESS");
		vaCity = "" + voResult.get("CITY_CODE");
		vaDistrict = "" + voResult.get("DISTRICT_CODE");
		vaProvince = "" + voResult.get("PROVINCE_CODE");
		vaPostCode = "" + voResult.get("POST_CODE");
		vaTelephone = "" + voResult.get("TELEPHONE");
		vaEmailAddress = "" + voResult.get("EMAIL_ADDRESS");
		vaStartWorking = "" + voResult.get("START_WORKING");
		vaEndWorking = "" + voResult.get("END_WORKING");
		vaUserPosition = "" + voResult.get("USER_POSITION");
		vaHeadPosition = "" + voResult.get("HEAD_POSITION");
		vaUserStatus = "" + voResult.get("USER_STATUS");
		vaSite = "" + voResult.get("SITE_ID");
		vaRole = "" + voResultUserRole.get("ROLE_ID");
		vaUserName = "" + voResult.get("USER_NAME");
		
		voObject.put("vaPName", vaPName.equals("null") ? "" : vaPName);
		voObject.put("vaFName", vaFName.equals("null") ? "" : vaFName);
		voObject.put("vaLName", vaLName.equals("null") ? "" : vaLName);
		voObject.put("vaBirthday", vaBirthday.equals("null") ? "" : JDate.showDate(vaBirthday));
		voObject.put("vaHomeNumber", vaHomeNumber.equals("null") ? "" : vaHomeNumber);
		voObject.put("vaAddress", vaAddress.equals("null") ? "" : vaAddress);
		voObject.put("vaCity", vaCity.equals("null") ? "" : vaCity);
		voObject.put("vaDistrict", vaDistrict.equals("null") ? "" : vaDistrict);
		voObject.put("vaProvince", vaProvince.equals("null") ? "" : vaProvince);
		voObject.put("vaPostCode", vaPostCode.equals("null") ? "" : vaPostCode);
		voObject.put("vaTelephone", vaTelephone.equals("null") ? "" : vaTelephone);
		voObject.put("vaEmailAddress", vaEmailAddress.equals("null") ? "" : vaEmailAddress);
		voObject.put("vaStartWorking", vaStartWorking.equals("null") ? "" : JDate.showDate(vaStartWorking));
		voObject.put("vaEndWorking", vaEndWorking.equals("null") ? "" : JDate.showDate(vaEndWorking));
		voObject.put("vaUserPosition", vaUserPosition.equals("null") ? "" : vaUserPosition);
		voObject.put("vaHeadPosition", vaHeadPosition.equals("null") ? "" : vaHeadPosition);
		voObject.put("vaUserStatus", vaUserStatus.equals("null") ? "" : vaUserStatus);
		voObject.put("vaSite", vaSite.equals("0") ? "" : vaSite);
		voObject.put("vaRole", vaRole.equals("0") ? "" : vaRole);
		voObject.put("vaUserName", vaUserName);
		
		db.close();
		voOut.print(voObject.toJSONString());
	}
    
    @SuppressWarnings("unchecked")
	public void editData(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		int  vnInsertUserId = Integer.parseInt(request.getSession().getAttribute("user_id").toString());
		
		PrintWriter voOut = response.getWriter();
		JSONObject voObject = new JSONObject();
		this.db = new Database();
		UserLoginDAO voUserLoginDAO = new UserLoginDAO(db);
		SMUserRoleDAO voSMUserRoleDAO = new SMUserRoleDAO(db);
		UserLogin voUserLogin = new UserLogin();
		SMUserRole voSMUserRole = new SMUserRole();
		String vaUserId = request.getParameter("vaUserId");
		String vaUserPName = request.getParameter("vaUserPName");
		String vaUserFName = request.getParameter("vaUserFName");
		String vaUserLName = request.getParameter("vaUserLName");
		String vaBirthDay = request.getParameter("vaBirthDay");
		String vaHomeNumber = request.getParameter("vaHomeNumber");
		String vaAddress = request.getParameter("vaAddress");
		String vaCity = request.getParameter("vaCity");
		String vaDistrict = request.getParameter("vaDistrict");
		String vaProvince = request.getParameter("vaProvince");
		String vaPostCode = request.getParameter("vaPostCode");
		String vaTelephone = request.getParameter("vaTelephone");
		String vaEmailAddress = request.getParameter("vaEmailAddress");
		String vaStartWork = request.getParameter("vaStartWork");
		String vaEndWork = request.getParameter("vaEndWork");
		String vaPosition = request.getParameter("vaPosition");
		String vaChiefPosition = request.getParameter("vaChiefPosition");
		String vaStatus = request.getParameter("vaStatus");
		String vaSite = request.getParameter("vaSite");
		String vaRole = request.getParameter("vaRole");
		String vaUsername = request.getParameter("vaUsername");
		
		// set ค่า LoginUser
		voUserLogin.setVnSiteId(Integer.parseInt(vaSite));
		voUserLogin.setVaUserId(Integer.parseInt(vaUserId));
		voUserLogin.setVaUserPName(vaUserPName.equals("") ? "" : vaUserPName);
		voUserLogin.setVaUserFName(vaUserFName.equals("") ? "" : vaUserFName);
		voUserLogin.setVaUserLName(vaUserLName.equals("") ? "" : vaUserLName);
		voUserLogin.setVaBirthDay(vaBirthDay.equals("") ? "" : JDate.saveDate(vaBirthDay));
		voUserLogin.setVaHomeNumber(vaHomeNumber.equals("") ? "" : vaHomeNumber);
		voUserLogin.setVaAddress(vaAddress.equals("") ? "" : vaAddress);
		voUserLogin.setVaCityCode(vaCity.equals("") ? "" : vaCity);
		voUserLogin.setVaDistrictCode(vaDistrict.equals("") ? "" : vaDistrict);
		voUserLogin.setVaProvinceCode(vaProvince.equals("") ? "" : vaProvince);
		voUserLogin.setVaPostCode(vaPostCode.equals("") ? "" : vaPostCode);
		voUserLogin.setVaTelephone(vaTelephone.equals("") ? "" : vaTelephone);
		voUserLogin.setVaEmailAddress(vaEmailAddress.equals("") ? "" : vaEmailAddress);
		voUserLogin.setVaStartWorking(vaStartWork.equals("") ? "" : JDate.saveDate(vaStartWork));
		voUserLogin.setVaEndWorking(vaEndWork.equals("") ? "" : JDate.saveDate(vaEndWork));
		voUserLogin.setVaUserPosition(vaPosition.equals("") ? "" : vaPosition);
		voUserLogin.setVaHeadPosition(vaChiefPosition.equals("") ? "" : vaChiefPosition);
		voUserLogin.setVaUserStatus(vaStatus.equals("") ? "" : vaStatus);
		voUserLogin.setVaUsername(vaUsername.equals("") ? "" : vaUsername);
		voUserLogin.setVnUpdateUserId(vnInsertUserId);
		voUserLogin.setVaUpdateDate(JDate.getDate());
		voUserLogin.setVaUpdateTime(JDate.getTime());

		// set ค่า SMUserRole
		voSMUserRole.setVnUserId(Integer.parseInt(vaUserId));
		voSMUserRole.setVaRoleId(vaRole);
		voSMUserRole.setVnUpdateUserId(vnInsertUserId);
		voSMUserRole.setVaUpdateDate(JDate.getDate());
		voSMUserRole.setVaUpdateTime(JDate.getTime());
		
		try{
			db.beginTransaction();
			boolean result = false;
			result = voUserLoginDAO.updateUserLogin(voUserLogin);
			if (result) {
				result = voSMUserRoleDAO.updateUserRole(voSMUserRole);
				if (result) {
					voObject.put("resultData", "success");
					db.commit();
				} else {
					voObject.put("resultData", "fail");
				}
			}
			db.commit();
		} catch (Exception e) {
			SMErrorLogDAO.getInstance().addLog(0, "CS83001", JDate.getDate(), JDate.getTime(),vnInsertUserId, "CS83001Srvl", e.getMessage(),vaUrlAddress, vaAgentBrowser);
			db.rollback();
			voObject.put("resultData", "fail");
		}
		db.close();
		voOut.print(voObject.toJSONString());
	}
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

}
